package ru.tennisworld.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.tennisworld.user.dto.RegistrationTennisUserRequestDto;
import ru.tennisworld.user.dto.TennisUserFullInfoResponseDto;
import ru.tennisworld.user.dto.TennisUserResponseDto;
import ru.tennisworld.user.dto.UpdateTennisUserRequestDto;
import ru.tennisworld.user.entities.TennisUser;
import ru.tennisworld.user.mappers.TennisUserMapper;
import ru.tennisworld.user.service.TennisUserControllerService;
import ru.tennisworld.user.service.users.TennisUserService;

import java.util.List;

@Service("authControllerService")
public class TennisUserControllerServiceImpl implements TennisUserControllerService {

    @Autowired
    private TennisUserService tennisUserService;

    @Autowired
    private TennisUserMapper tennisUserMapper;

    private static final Logger LOG = LoggerFactory.getLogger(TennisUserControllerServiceImpl.class);

    @Override
    public List<TennisUserResponseDto> getUserReference(List<String> usernameList) {
        LOG.info("operation=TennisUserControllerServiceImpl.getUserReference, action=start, usernameList={}", usernameList);
        List<TennisUser> tennisUserList = tennisUserService.getUserReferenceList(usernameList);
        List<TennisUserResponseDto> result = tennisUserMapper.mapToReferenceUserDto(tennisUserList);
        LOG.info("operation=TennisUserControllerServiceImpl.getUserReference, action=end, result={}", result);
        return result;
    }

    @Override
    public TennisUserFullInfoResponseDto getUserInfo(final String username) {
        LOG.info("operation=TennisUserControllerServiceImpl.getUserInfo, action=start, userId={}", username);
        TennisUser tennisUser = tennisUserService.getTennisUser(username);
        TennisUserFullInfoResponseDto result = tennisUserMapper.mapToDto(tennisUser);
        LOG.info("operation=TennisUserControllerServiceImpl.getUserInfo, action=end, result={}", result);
        return result;
    }

    @Override
    public TennisUserResponseDto createNewUser(RegistrationTennisUserRequestDto request) {
        LOG.info("operation=TennisUserControllerServiceImpl.createNewUser, action=start, request={}", request);
        TennisUser newTennisUser = tennisUserMapper.mapToEntity(request);
        newTennisUser = tennisUserService.createNewServiceUser(newTennisUser);
        TennisUserResponseDto result = tennisUserMapper.mapToUserDto(newTennisUser);
        LOG.info("operation=TennisUserControllerServiceImpl.createNewUser, action=end");
        return result;
    }

    @Override
    public TennisUserResponseDto updateTennisUser(String username, UpdateTennisUserRequestDto request) {
        LOG.info("operation=TennisUserControllerServiceImpl.createNewUser, action=start, request={}", request);
        TennisUser updateTennisUser = tennisUserService.getTennisUser(username);
        TennisUser srcTennisUser = tennisUserMapper.mapToEntity(request);
        updateTennisUser = tennisUserService.updateTennisUserMainInfo(updateTennisUser, srcTennisUser);
        TennisUserResponseDto result = tennisUserMapper.mapToUserDto(updateTennisUser);
        LOG.info("operation=TennisUserControllerServiceImpl.createNewUser, action=end");
        return result;
    }
}
